
#include <stdlib.h>

#include <apr_strings.h>
#include <apr_uuid.h>

#include "ssdp.h"
#include "config.h"

#define DEFAULT_NAME	"MediaServer"
#define DEFAULT_UUID	"00112233-4455-6677-8899-aabbccddeeff"
#define DEFAULT_SERIAL	123456

static const char*
dlna_cmd_identity(cmd_parms* parms, void* mconfig, const char* arg1, const char* arg2, const char* arg3)
{
	dlna_dir_config* cfg = (dlna_dir_config*) mconfig;

	apr_uuid_t uuid;
	if (! apr_uuid_parse(&uuid, arg2)) {
		return "invalid uuid string.";
	}

	cfg->name = (char*)apr_pstrdup(parms->pool, arg1);

	cfg->uuid = (char*)apr_pcalloc(parms->pool, APR_UUID_FORMATTED_LENGTH + 1);
	apr_uuid_format(cfg->uuid, &uuid);

	cfg->serial = atoi(arg3);

	register_ssdpd(parms->temp_pool, parms->server, cfg->uuid);

	return NULL;
}

command_rec
dlna_cmds[] = {
	{
		"Identity",	// directive name
		dlna_cmd_identity, // handler
		NULL,
		ACCESS_CONF,
		TAKE3,
		"dlna FriendlyName, uuid and serialnumber"
	},
	{NULL},
};


void*
dlna_create_dir_config(apr_pool_t* p, char* dir)
{
	dlna_dir_config* cfg = apr_pcalloc(p, sizeof(dlna_dir_config));

	cfg->name = DEFAULT_NAME;
	cfg->uuid = DEFAULT_UUID;
	cfg->serial = DEFAULT_SERIAL;

	return cfg;
}
